package screen;

import java.util.Vector;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import main.GameCanvas;
import main.NuoiThuMidlet;
import model.Animal;
import model.AnimalInfo;
import model.Avatar;
import model.AvatarInfo;
import model.Command;
import model.DataManager;
import model.DrugInfo;
import model.FoodInfo;
import model.IAction;
import model.Item;
import model.ItemInfo;
import model.MoneyInfo;
import model.MyObj;
import model.PlayerInfo;
import model.Position;
import model.Toy;
import model.ToyInfo;
import service.GlobalService;
import utilities.CRes;
import utilities.GameResource;
import utilities.TileMap;
import animation.ExplosionStar;
import animation.FallingStar;
import animation.FlyText;
import animation.Smoke;
import font.BitmapFont;

public class MainScr extends Screen {
	public static final int SERVICEID_MAIN = 0;
	public static final int SERVICEID_GAME_TIM_CAP = 1;
	public static final int SERVICEID_GAME_QUAY_SO = 2;
	public static final int SERVICEID_GAME_DUA_THU = 3;
	public static final int SERVICEID_GAME_SONG_DAU = 4;
	public static final int SERVICEID_GAME_HOC_TIENG_ANH = 5;
	public static final int SERVICEID_GAME_BAU_CUA = 6;
	public static final int SERVICEID_GAME_DOAN_THU = 7;

    public PlayerInfo m_friend = null;
   
    //public Position m_posFood = new Position(50,200);
    
    //selection of animal
    public Animal m_selectedAnimal = null;
    public boolean m_bIsSelectedPet = false;
    
    //selection of toy
    public boolean m_bIsSelectedToy = false;
    public Toy m_selectedToy = null;
    
    public boolean m_bIsFocusToy = false;
    
    private int countDown = 0;
    int m_idFarmOwner;    
    private int m_iPreTileX;
    private int m_iPreTileY;
    
    private int m_thecaoid = 0;
    
    Vector m_arrAnimal = new Vector();
    Vector m_arrToy = new Vector();
    Vector m_arrSelectAnimal = new Vector();
    
    Image m_imgBackground;
    
    private Command menuCmd;
    private Command veNhaCmd;
    
    //main menu icon
    public Command shopCmd;
    public Command stockCmd;
    private Command petCmd;
    private Command toyCmd;
    private Command miniGameCmd;
    private Command friendCmd;
    private Command questCmd;
    
    //pet menu icon
    private Command feedCmd;
    private Command petInfoCmd;
    private Command takeABathCmd;
    private Command goDoctorCmd;
    private Command harvestCmd;
    private Command sellPetCmd;
    
    //mini game command
    private Command songDauCmd;
    //private Command duaThuCmd;
    private Command hocTiengAnhCmd;
    private Command quaySoCmd;
    private Command bauCuaCmd;
    private Command doanThuCmd;
    //private Command timCapCmd;
    
    //other commands
    private Command exitPetMenuCmd;
    private Command mainControlCmd;
    
    private Command setPosToyCmd;
    private Command exitToyCmd;
    private Command selectToyCmd;
    private Command storeToyCmd;
    ///////command quay so
    private Command starQuaySoCmd;
    private Command nextQuaySoCmd;
    private Command exitQuaySocmd;
        
    ///////////sky garden///////////
    public int selectedRankIndex = -1;
    public int taskprice;
    
    private int m_iTimeChat = 1000;
    private static String[] m_arrChatContent = {"Chán wá, đi chơi game đi", 
    	"Hôm nay bạn có khỏe không?", 
    	"hehehe",
    	"vườn nhà mình đẹp quá ^^",
    	"Trời hôm nay đẹp ghê", 
    	"Hix gần đói bụng nữa rùi",
    	"hello", 
    	"Mình thật là đẹp",
    	"8",
    	"9",
    	"10",
    	"11",
    	"12",
    	"13",
    	"14",
    	"Ta là sát thủ đầu mưng mủ đây !!!",
    	"Hận đời vô đối",
    	"Học, học nữa, học mãi ... hộc máu ... => chơi game đi" ,
    	"Giang hồ hiểm ác không bằng mạng lag thất thường",
    	"Phận làm trai gõ phím bình thiên hạ. Thân anh hùng click chuột định giang sơn",
    	"Mấy đời bánh đúc có xương. Mấy đời chơi Net không vương tơ tình",
    	"Thà sống hèn còn hơn chết dại",
    	"hihihihi",
    	"buồn quá đi huhuhu"
    	};
    
  
    static MainScr instance = null;
    
    public static MainScr gI() {
        if (instance == null) {
            instance = new MainScr();
        }
        return instance;        
    }
    
    public void switchToMe(){
        TileMap.instance.loadMap(TileMap.GARDEN_MAP);
        super.switchToMe();
    }
        
    //set garden data
    public void setGardenData(int farmOwner, Vector listAnimals, Vector listToy) {
    	////
    	m_arrChatContent[8] = "Hôm nay, "+NuoiThuMidlet.m_myPlayerInfo.m_strName+" khỏe không ?";
    	m_arrChatContent[9] = NuoiThuMidlet.m_myPlayerInfo.m_strName+" ơi, mình chơi game đi";
    	m_arrChatContent[10] = "Woooo, "+NuoiThuMidlet.m_myPlayerInfo.m_strName+" nhìn trông đẹp quá !";
    	m_arrChatContent[11] = "Hix, mình đói quá "+NuoiThuMidlet.m_myPlayerInfo.m_strName+" à ?";
    	m_arrChatContent[12] = "Hôm nay, trời đẹp "+NuoiThuMidlet.m_myPlayerInfo.m_strName+" nhỉ ?";
    	m_arrChatContent[13] = "Chúc "+NuoiThuMidlet.m_myPlayerInfo.m_strName+" một ngày tốt lành !";
    	m_arrChatContent[14] = "Nhìn "+NuoiThuMidlet.m_myPlayerInfo.m_strName+" ngộ ghê !";
    	////
    	m_idFarmOwner = farmOwner;
    	
        //stop waiting dialog
        GameCanvas.endDlg();
               
        CRes.cleanVector(m_arrAnimal);
        m_arrAnimal = listAnimals;
        
        orderAnimalById();
		
        
        TileMap.instance.loadMap(TileMap.GARDEN_MAP);
        int tmpY = TileMap.m_iHeightTile * TileMap.instance.m_gardenMap.m_iMapHeight;
        int tmpYY = GameCanvas.h - tmpY*2/3 + 10;       
              
        int i, count = m_arrAnimal.size();
        for (i = 0; i < count; i++) {
            Animal ani = (Animal) m_arrAnimal.elementAt(i);
            ani.m_iLeft = 50;
            ani.m_iTop = tmpYY +35;
            ani.m_iBoundH = tmpY*2/3 - GameCanvas.hBottomBar - 45;
            ani.m_iBoundW = GameCanvas.w -70;
            
        }
        
        m_arrToy = null;
        m_arrToy = orderToy(listToy);
        
        if(m_idFarmOwner == NuoiThuMidlet.m_myPlayerInfo.m_iId && m_arrAnimal.size() > 0){
    		NuoiThuMidlet.m_myPlayerInfo.m_defaultPet = (Animal)m_arrAnimal.elementAt(0);
    	}
        // System.gc();
        
        m_bIsSelectedPet = false;
        
        if(m_idFarmOwner != NuoiThuMidlet.m_myPlayerInfo.m_iId){
    		m_cmdRight = veNhaCmd;
    	}
    	else
    		m_cmdRight = null;
    }
    public int m_iPosXName;
    public MainScr() {    	
    	//init TileMap    	
        TileMap.instance.loadMap(TileMap.GARDEN_MAP);
        
        //init background
        m_imgBackground = Image.createImage(GameCanvas.w, GameCanvas.h);
        Graphics g = m_imgBackground.getGraphics();
        g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
        
        int hMap = TileMap.m_iHeightTile * TileMap.instance.m_gardenMap.m_iMapHeight;
        g.setColor(0x42b4ff);
        g.fillRect(0, 0, GameCanvas.w, GameCanvas.h);
        TileMap.instance.paintMap(g, 0, GameCanvas.h - hMap);
        int tmpY = TileMap.m_iHeightTile * TileMap.instance.m_gardenMap.m_iMapHeight;
        tmpY = GameCanvas.h - tmpY*2/3 + 10;
        paintGate(g, 0, tmpY);
        
        int xfeeder = 5;
		int yfeeder = GameCanvas.h - GameCanvas.hBottomBar - GameResource.instance.imgFeeder.getHeight() - 50;
		g.drawImage(GameResource.instance.imgFeeder, xfeeder, yfeeder, Graphics.LEFT|Graphics.TOP);
		//g.drawImage(GameResource.instance.imgFood, xfeeder+3, yfeeder+6, Graphics.LEFT|Graphics.TOP);
		g.drawImage(GameResource.instance.imgBathTube, GameCanvas.w - GameResource.instance.imgBathTube.getWidth()-5, tmpY+40, Graphics.LEFT| Graphics.BOTTOM);
        		
        //init command
        initCommand();
        
        //initialize command
        menuCmd = new Command(GameResource.menu, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                Vector menu = new Vector();

                /*
                menu.addElement(new Command(GameResource.chargeMoney, new IAction() {

                    public void perform() {
                        // TODO Auto-generated method stub
                    	if (DataManager.gI().m_vtSMS == null) {
                            //request list sms
                            GlobalService.gI().doGetListSMS();
                            GameCanvas.startWaitDlg(GameResource.plzWait);
                        } else {
                            //display list sms
                            onListSms(DataManager.gI().m_vtSMS);
                        }
                    }
                }));

                menu.addElement(new Command(GameResource.chargeMoneySimCard, new IAction() {

                    public void perform() {
                    	final TField[] list = new TField[2];
                		String[] name = new String[2];
                		name[0] = "Mã số thẻ:";
                		name[1] = "Số seri:";
                		for (int i = 0; i < 2; i++) {
                			list[i] = new TField();
                		}
                		list[0].setIputType(TField.INPUT_TYPE_ANY);
                		list[1].setIputType(TField.INPUT_TYPE_NUMERIC);
                		Command cmd = new Command("Gửi", new IAction() {
                			public void perform() {
                				if (list[1].getText().equals("")) {
                					GameCanvas.startOKDlg("Bạn phải nhập số seri.");
                					return;
                				}
                				if (list[0].getText().equals("")) {
                					GameCanvas.startOKDlg("Bạn phải nhập mã số thẻ.");
                					return;
                				}
                				
                				GlobalService.gI().doRequestChargeMoneySimCard(NuoiThuMidlet.m_myPlayerInfo.m_strName, m_thecaoid, list[0].getText(), list[1].getText());
                			}
                		});
                		
                		Command left = new Command(GameResource.select, new IAction() {
							
							public void perform() {
								// TODO Auto-generated method stub
								Vector menuItem = new Vector();
								
								Command c = new Command(GameResource.mobi, new IAction() {
									
									public void perform() {
										// TODO Auto-generated method stub
										InputScr.gI().subTitle = GameResource.mobi;
										m_thecaoid = 0;
									}
								});
								menuItem.addElement(c);
								
								c = new Command(GameResource.vina, new IAction() {
									
									public void perform() {
										// TODO Auto-generated method stub
										InputScr.gI().subTitle = GameResource.vina;
										m_thecaoid = 1;
									}
								});
								menuItem.addElement(c);
								
								c = new Command(GameResource.viettel, new IAction() {
									
									public void perform() {
										// TODO Auto-generated method stub
										InputScr.gI().subTitle = GameResource.viettel;
										m_thecaoid = 2;
									}
								});
								menuItem.addElement(c);
								
								c = new Command(GameResource.vtc, new IAction() {
									
									public void perform() {
										// TODO Auto-generated method stub
										InputScr.gI().subTitle = GameResource.vtc;
										m_thecaoid = 3;
									}
								});
								menuItem.addElement(c);
								
								c = new Command(GameResource.fpt, new IAction() {
									
									public void perform() {
										// TODO Auto-generated method stub
										InputScr.gI().subTitle = GameResource.fpt;
										m_thecaoid = 4;
									}
								});
								menuItem.addElement(c);
								
								c = new Command(GameResource.close, new IAction() {
									
									public void perform() {
										// TODO Auto-generated method stub
										InputScr.gI().close();
									}
								});
								menuItem.addElement(c);
	
								GameCanvas.menu.startAt(menuItem, 0);
							}
						});
                    	
                		m_thecaoid = 0;
                    	InputScr.gI().setInfo(list, name, cmd, left, GameResource.chargeMoneySimCard, GameResource.mobi);
                    	InputScr.gI().switchToMe(MainScr.gI());
                    }
                }));
				*/

                menu.addElement(m_viewMsgCmd);

                menu.addElement(new Command(GameResource.diary, new IAction() {
                    public void perform() {
                        //request diary
                        GameCanvas.startWaitDlg(GameResource.plzWait);
                        GlobalService.gI().doGetDiary();
                    }
                }));

                menu.addElement(new Command(GameResource.playerInfo, new IAction() {

                    public void perform() {
                    	ProfileScr.gI().switchToMe(NuoiThuMidlet.m_myPlayerInfo);
                    }
                }));

                menu.addElement(new Command(GameResource.changePass, new IAction() {

                    public void perform() {
                    	ChangePassScr.gI().switchToMe();
                    }
                }));

                menu.addElement(new Command(GameResource.rank, new IAction() {

                    public void perform() {
                    	GameCanvas.startWaitDlg(GameResource.plzWait);
                        GlobalService.gI().doRequestTopPlayer(1);
                    	
                    	/*
                        if (DataManager.gI().m_ListHighLevel == null) {
                            //request list
                            GameCanvas.startWaitDlg(GameResource.plzWait);
                            GlobalService.gI().doViewTopPlayer(0);
                        } else {
                            //show list
                            selectedRankIndex = 0;
                            onListTopPlayers(DataManager.gI().m_ListHighLevel);
                        }
                        */
                    }
                }));

                
                menu.addElement(new Command(GameResource.forum, new IAction() {
                    public void perform() {
                        GameCanvas.startOKDlg(GameResource.doYouWantGoToForum, new IAction() {
                            public void perform() {
                            	
                            	if(DataManager.gI().m_strForumLink == null){
                            		GlobalService.gI().doRequestForumLink();
                            	}
                            	else
                            	{
                            		onReceivedForumLink(DataManager.gI().m_strForumLink);
                            	}
                            	/*
                            	try {
                                    GameCanvas.endDlg();
                                    String url = "http://m.taigamejava.com/";
                                    NuoiThuMidlet.instance.platformRequest(url);
                                    close();
                                    NuoiThuMidlet.instance.notifyDestroyed();
                                } catch (ConnectionNotFoundException e) {
                                    e.printStackTrace();
                                }
                                */
                            }							
                        });
                    }
                }));

                
                menu.addElement(new Command(GameResource.exit, new IAction() {

                    public void perform() {
                        GameCanvas.startOKDlg(GameResource.doYouWantExit, new IAction() {
                            //log out

                            public void perform() {
                                close();

                                //clear cache data
                                DataManager.gI().cleanUserCache();
                                
                                //clear old message
                                //MsgScreen.gI().tabs.removeAllElements();
                                
                                //release mem
                            	System.gc();
                            }
                        });
                    }
                }));

                //show menu
                GameCanvas.menu.startAt(menu, 0);
            }
        });

        
        veNhaCmd = new Command(GameResource.goHome, new IAction() {
            //go home
            public void perform() {
                // TODO Auto-generated method stub
                GameCanvas.mainScr.m_friend = null;
                GameCanvas.mainScr.m_cmdRight = null;

                GameCanvas.startWaitDlg(GameResource.plzWait);
                GlobalService.gI().getGardenData(NuoiThuMidlet.m_myPlayerInfo.m_iId);
            }
        });

        m_cmdLeft = menuCmd;
        m_cmdCenter = mainControlCmd;
        
        //run gc
        System.gc();
    }

    //sort list animal
	public Vector orderAnimal(Vector vtAnimal) {
		int count = vtAnimal.size();
		for (int i = 0; i < count - 1; i++) {
			Animal ani = (Animal) vtAnimal.elementAt(i);
			for (int j = i + 1; j < count; j++) {
				Animal ani1 = (Animal) vtAnimal.elementAt(j);
				if (ani.m_position.y > ani1.m_position.y) {
					vtAnimal.setElementAt(ani, j);
					vtAnimal.setElementAt(ani1, i);
					ani = ani1;
				}
			}
		}
		return vtAnimal;
	}
	
	public void orderAnimalById() {
		m_arrSelectAnimal.removeAllElements();
		int i;
		for(i = 0; i < m_arrAnimal.size(); i++){
			Animal ani = (Animal)m_arrAnimal.elementAt(i);
			Animal newAni = new Animal(ani.animalInfo.m_iId);
			ani.copyTo(newAni);
			m_arrSelectAnimal.addElement(newAni);
		}
				
		int count = m_arrSelectAnimal.size();
		for (i = 0; i < count - 1; i++) {
			Animal ani = (Animal) m_arrSelectAnimal.elementAt(i);
			for (int j = i + 1; j < count; j++) {
				Animal ani1 = (Animal) m_arrSelectAnimal.elementAt(j);
				if (ani.animalInfo.m_iId > ani1.animalInfo.m_iId) {
					m_arrSelectAnimal.setElementAt(ani, j);
					m_arrSelectAnimal.setElementAt(ani1, i);
					ani = ani1;
				}
				if (ani.animalInfo.m_iId == ani1.animalInfo.m_iId && ani.m_iId > ani1.m_iId) {
					m_arrSelectAnimal.setElementAt(ani, j);
					m_arrSelectAnimal.setElementAt(ani1, i);
					ani = ani1;
				}
			}
		}
	}
	
	
	//sort list toy
	public Vector orderToy(Vector vtToy) {
		int count = vtToy.size();
		for (int i = 0; i < count - 1; i++) {
			Toy ani = (Toy) vtToy.elementAt(i);
			for (int j = i + 1; j < count; j++) {
				Toy ani1 = (Toy) vtToy.elementAt(j);
				if (ani.m_Y > ani1.m_Y) {
					vtToy.setElementAt(ani, j);
					vtToy.setElementAt(ani1, i);
					ani = ani1;
				}
			}
		}
		return vtToy;
	}
			
    //show shop
    public void goShopping(int focusTab, int focusIndex) {
    	if(GameCanvas.currentScreen != ShopScr.gI()){
    		ShopScr.gI().switchToMe();	
    	}
    	ShopScr.gI().isShowStock = false;
        ShopScr.gI().setShopItems(
                new Vector[]{
                		DataManager.gI().getListAnimalInfo(),
                		DataManager.gI().getListFoodInfo(),
                		DataManager.gI().getListDrugInfo(),
                		DataManager.gI().getListToyInfo(),
                		DataManager.gI().getListAvatarInfo()},
                new Command(GameResource.select, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
            	if(ShopScr.gI().isShowStock){
            		doUseItemInStock();
            	}
            	else
            		doBuyItem();
            }
        }));
        System.out.println("focus index " + focusIndex);
        ShopScr.gI().focusTab = focusTab;
        ShopScr.gI().focus = focusIndex;
        ShopScr.gI().m_cmdLeft = stockCmd;
    }
    
    //show stock
    public void goMyStock(int focusTab) {
    	if(GameCanvas.currentScreen != ShopScr.gI()){
    		ShopScr.gI().switchToMe();	
    	}
    		    	
        ShopScr.gI().isShowStock = true;
        ShopScr.gI().setShopItems(
                new Vector[]{
                		m_arrSelectAnimal,
                		DataManager.gI().m_myLstFood, 
                		DataManager.gI().m_myLstDrug,
                		DataManager.gI().m_myLstToy,
                		DataManager.gI().m_myLstAvatar},
                new Command(GameResource.select, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
            	if(ShopScr.gI().isShowStock){
            		doUseItemInStock();
            	}
            }
        }));
        ShopScr.gI().focusTab = focusTab;
        ShopScr.gI().m_cmdLeft = shopCmd;
    }
    
    //return login screen
    public void close() {
        // TODO Auto-generated method stub    	
        GameCanvas.mainScr.m_friend = null;
        GameCanvas.mainScr.m_cmdRight = null;
        
        GlobalService.gI().disconnect();
        GameCanvas.loginScr.switchToMe();
        GameCanvas.endDlg();
    }

    private void initCommand() {
    	//init other commands
    	mainControlCmd = new Command(GameResource.select, new IAction() {			
			public void perform() {
				// TODO Auto-generated method stub
				if(m_bIsSelectedToy || m_bIsFocusToy){
	        		m_cmdCenter.action.perform();
	        	}
	        	else 
	        	{
	        		Vector menu = new Vector();

	            	if(m_bIsSelectedPet){
	            		menu.addElement(feedCmd);
	    	            menu.addElement(petInfoCmd);
	    	            menu.addElement(takeABathCmd);
	    	            menu.addElement(goDoctorCmd);
	    	            
	    	            if(m_selectedAnimal != null && m_selectedAnimal.m_bIsHaveMoney){
	    	            	menu.addElement(harvestCmd);
	    		        }
	    	            if(m_idFarmOwner == NuoiThuMidlet.m_myPlayerInfo.m_iId)
	    	            	menu.addElement(sellPetCmd);
	    	        }
	            	else
	            	{
	            		if(m_idFarmOwner == NuoiThuMidlet.m_myPlayerInfo.m_iId){
	        	            menu.addElement(shopCmd);
	        	            menu.addElement(stockCmd);
	        	            menu.addElement(toyCmd);
		                }
	                    menu.addElement(petCmd);
	                    if(m_idFarmOwner == NuoiThuMidlet.m_myPlayerInfo.m_iId){
	                    	menu.addElement(miniGameCmd);
		                }
	                    menu.addElement(friendCmd);
	                    menu.addElement(questCmd);
	                }
	                GameCanvas.menu.startMenuGarden(menu, GameCanvas.hw, GameCanvas.h - 65, 32);
	           }
			}
		}    	
    	){
    		public void paint(Graphics g, int x, int y){
				g.drawImage(GameResource.instance.imgMesssage, x, y, 0);
			}
    	};
    	   	
    	exitPetMenuCmd = new Command(GameResource.close, new IAction() {
			public void perform() {
				// TODO Auto-generated method stub
		    	m_bIsSelectedPet = false;
		    	GameCanvas.menu.m_showMenuGarden = false;
		    	GameCanvas.menu.m_showMenu = false;

		    	m_selectedAnimal.m_bIsFocus = false;
		    	m_selectedAnimal = null;
		    	
		    	if(m_idFarmOwner != NuoiThuMidlet.m_myPlayerInfo.m_iId){
		    		m_cmdRight = veNhaCmd;
		    	}
		    	else
		    		m_cmdRight = null;
			}
		});
    	
    	setPosToyCmd = new Command("Đặt", new IAction() {
			public void perform() {
				// TODO Auto-generated method stub
				//check
				m_cmdLeft = menuCmd;
				int i;
				for(i = 0; i < m_arrToy.size(); i++){
					Toy t = (Toy)m_arrToy.elementAt(i);
					if(t != m_selectedToy && t.m_iTileX == m_selectedToy.m_iTileX && t.m_iTileY == m_selectedToy.m_iTileY){
						GameCanvas.startOKDlg("Vị trí này đã có vật phẩm");
						return;
					}
				}
				
				//send to server
				GameCanvas.startWaitDlg(GameResource.plzWait);
				GlobalService.gI().doPutToy(m_idFarmOwner, m_selectedToy.itemInfo.m_iId, m_selectedToy.m_iId, m_selectedToy.m_iTileX, m_selectedToy.m_iTileY);
			}
		});
    	
    	selectToyCmd = new Command(GameResource.select, new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
			
				m_bIsFocusToy = false;
				m_bIsSelectedToy = true;
				
				m_iPreTileX = m_selectedToy.m_iTileX;
				m_iPreTileY = m_selectedToy.m_iTileY;
				
	        	m_cmdCenter = setPosToyCmd;
	        	m_cmdRight = exitToyCmd;
	        	m_cmdLeft = storeToyCmd;
			}
		});
    	
    	exitToyCmd = new Command(GameResource.exit, new IAction() {
			public void perform() {
				// TODO Auto-generated method stub
				if(m_bIsSelectedToy){
					m_selectedToy.m_iTileX = (byte)m_iPreTileX;
					m_selectedToy.m_iTileY = (byte)m_iPreTileY;
				}
				
				m_bIsSelectedToy = false;
				m_bIsFocusToy = false;
				m_selectedToy.m_bIsFocus = false;
			
				if(DataManager.gI().m_myLstToy.contains(m_selectedToy)){
					m_selectedToy.m_iTileX = -1;
					m_selectedToy.m_iTileY = -1;
					m_selectedToy.m_X = 0;
					m_selectedToy.m_Y = 0;	
				}
				
				m_selectedToy = null;
				m_cmdCenter = null;
				m_cmdRight = null;
				m_cmdLeft = menuCmd;
				m_cmdCenter = mainControlCmd;
			}
		});
    	
    	
    	
    	storeToyCmd = new Command("Cất", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				GameCanvas.startOK("Bạn có muốn cất đồ chơi này vào trong kho không?", new IAction() {
					
					public void perform() {
						// TODO Auto-generated method stub
						GameCanvas.endDlg();
						
						if(m_bIsSelectedToy){
							m_selectedToy.m_iTileX = -1;
							m_selectedToy.m_iTileY = -1;
							m_selectedToy.m_X = 0;
							m_selectedToy.m_Y = 0;
						}
						
						m_bIsSelectedToy = false;
						m_bIsFocusToy = false;
						m_selectedToy.m_bIsFocus = false;
						
						//send to server
						GameCanvas.startWaitDlg(GameResource.plzWait);
						GlobalService.gI().doPutToy(m_idFarmOwner, m_selectedToy.itemInfo.m_iId, m_selectedToy.m_iId, (short)-1, (short)-1);
						
						//reset
						m_arrToy.removeElement(m_selectedToy);
						DataManager.gI().m_myLstToy.addElement(m_selectedToy);
						
						m_selectedToy = null;
						m_cmdCenter = null;
						m_cmdRight = null;
						m_cmdLeft = menuCmd;
						m_cmdCenter = mainControlCmd;
					}
				});
			}
		});
    	    	
    	//init main command
    	shopCmd = new Command(GameResource.shop, new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub				
				goShopping(0, 0);
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(0, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	stockCmd = new Command(GameResource.container, new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub				
				goMyStock(0);
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(23, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	    	
    	petCmd = new Command("Thú nuôi", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				if(m_arrAnimal.size() > 0){
					m_bIsSelectedPet = true;	         
					//m_iSelectedPetIndex = 0;
					m_selectedAnimal = (Animal)m_arrAnimal.elementAt(0);
					m_selectedAnimal.m_bIsFocus = true;

					//exit selected pet function
		            m_cmdRight = exitPetMenuCmd;
				}
				else{
					GameCanvas.startOK("Bạn chưa có vật nuôi. Vui lòng Mua tại cửa hàng", new IAction() {
						
						public void perform() {
							// TODO Auto-generated method stub
							GameCanvas.endDlg();
							goShopping(0, 0);		
						}
					});			    	
				}				
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(1, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};    	
    	    	
    	
    	toyCmd = new Command(GameResource.toy, new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
		    	if(m_arrToy.size() > 0){
		
		    		m_bIsFocusToy = true;
		    		m_bIsSelectedToy = false;
		        	m_selectedToy = (Toy)m_arrToy.elementAt(0);
		        	m_selectedToy.m_bIsFocus = true;
		        	
		        	m_cmdCenter = selectToyCmd;
		        	m_cmdRight = exitToyCmd;
		        }
		    	else
		    	{
		    		if(DataManager.gI().m_myLstToy.size() > 0){
		    			GameCanvas.startOK("Vui lòng vào kho để lấy vật phẩm để sử dụng", new IAction() {
							public void perform() {
								// TODO Auto-generated method stub
								GameCanvas.endDlg();
					    		goMyStock(3);
							}
						});
		    		}
		    		else
		    		{
		    			GameCanvas.startOK("Chưa có vật phẩm, vui lòng mua tại cửa hàng", new IAction() {
							public void perform() {
								// TODO Auto-generated method stub
								GameCanvas.endDlg();
					    		goShopping(3, 0);
							}
						});
		    		}
		    	}
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(5, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	miniGameCmd = new Command("Game", new IAction() {
			public void perform() {
				// TODO Auto-generated method stub
				if(m_arrAnimal.size() > 0){
					Vector menu = new Vector();
					
		            menu.addElement(songDauCmd);
		            //menu.addElement(duaThuCmd);
		            menu.addElement(hocTiengAnhCmd);
		            menu.addElement(quaySoCmd);
		            menu.addElement(bauCuaCmd);
		            menu.addElement(doanThuCmd);
		            //menu.addElement(timCapCmd);	            
		            
		            GameCanvas.menu.startMenuGarden(menu, GameCanvas.hw, GameCanvas.h - 65, 32);	
				}
				else
				{
					GameCanvas.startOK("Bạn chưa có vật nuôi. Vui lòng Mua tại cửa hàng", new IAction() {
						
						public void perform() {
							// TODO Auto-generated method stub
							GameCanvas.endDlg();
							goShopping(0, 0);		
						}
					});	
				}				
	        }
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(2, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	friendCmd = new Command(GameResource.friendlist, new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				if (DataManager.gI().m_myListFriend == null) {
                    //request friend list
                    GameCanvas.startWaitDlg(GameResource.plzWait);
                    GlobalService.gI().getFriendList();
                } else {
                    //show friend list
                    onFriendList(DataManager.gI().m_myListFriend);
                }
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(3, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	questCmd = new Command(GameResource.task, new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
			    GameCanvas.startWaitDlg(GameResource.plzWait);
                GlobalService.gI().getListCurrentQuest();
            }
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(4, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	//init pet menu icon command
    	feedCmd = new Command(GameResource.feed, new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				if(m_selectedAnimal != null){
					m_selectedAnimal.m_iGoToAction = 0;
					Item foodInfo = DataManager.gI().getMyItem(Item.ITEM_TYPE_FOOD, m_selectedAnimal.animalInfo.m_sFood);
					final int foodId = m_selectedAnimal.animalInfo.m_sFood - 1;
					
					if(foodInfo == null || foodInfo.m_iNumItem == 0){
						GameCanvas.startOKDlg("Không đủ thức ăn, mua tại cửa hàng", new IAction() {
							public void perform() {
								// TODO Auto-generated method stub
								goShopping(1, foodId);
								GameCanvas.endDlg();
							}
						});
					}
					else
					{
						//GlobalService.gI().feedAnimal(m_idFarmOwner, a.animalInfo.m_iId, a.m_iId, foodInfo.m_iId);
						//m_selectedAnimal.initToPosition(new Position(49, GameCanvas.hh + 80), 1);
						m_selectedAnimal.m_tmpIsinitPosition = false;
						m_selectedAnimal.m_iAction = Animal.EAT;
						m_selectedAnimal.m_iFeedFood = foodInfo.m_iId;
						m_selectedAnimal.m_iGoToAction = 1;
					}
				}
				
				//exit menu
				exitPetMenuCmd.action.perform();	            
	        }
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(6, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	petInfoCmd = new Command("Thông tin chi tiết", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				if(m_selectedAnimal == null)
					return;
								
				Animal myAnimal = new Animal(m_selectedAnimal.animalInfo.m_iId){
					public void paintInfo(Graphics g, int x, int y){
						paintIcon(g, x, y-10);
						BitmapFont.drawBoldFont(g, m_strName, x + animalInfo.m_iW[m_iAnimalStatus] + 5, y , 0x3a001e, Graphics.LEFT | Graphics.TOP);
						
						paintInfoAnimal(g, x+7, y+25);
					}
				};
				
				//gan tat ca cac thuoc tinh cho myAnimal
				m_selectedAnimal.copyTo(myAnimal);
				//....
				
				Command cR = new Command(GameResource.exit, new IAction() {
					public void perform(){
						InfoScr.gI().close();
					}
				});
				
				InfoScr.gI().switchToMe("Thông tin chi tiết", 160, 160, myAnimal, null, null, cR);
				
				exitPetMenuCmd.action.perform();
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(7, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	takeABathCmd = new Command("Tắm", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				if(m_selectedAnimal != null){
					m_selectedAnimal.m_iGoToAction = 0;
					//GlobalService.gI().takeABathPet(a.animalInfo.m_iId, a.m_iId);
					//int tmpY = TileMap.m_iHeightTile * TileMap.instance.m_gardenMap.m_iMapHeight;
			        //tmpY = GameCanvas.h - tmpY*2/3 + 10;
					//m_selectedAnimal.initToPosition(new Position(GameCanvas.w - 50, tmpY+40), 2);
					m_selectedAnimal.m_tmpIsinitPosition = false;
			        m_selectedAnimal.m_iAction = Animal.TAKEABATH;
				}
				
				exitPetMenuCmd.action.perform();
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(8, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	goDoctorCmd = new Command("Chữa bệnh", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				if(m_selectedAnimal != null){
					if(m_selectedAnimal.m_iSick != -1 && m_selectedAnimal.m_iSick != 0){
						final int numItem = DataManager.gI().getMyNumItem(Item.ITEM_TYPE_DRUG, m_selectedAnimal.m_iSick);
						final DrugInfo drugInfo = DataManager.gI().getDrugInfo(m_selectedAnimal.m_iSick);
						
						final Animal myAnimal;
						Command cL = null, cR = null, cC = null;
						if(numItem == 0){
							//show go to shop`	
							myAnimal = new Animal(m_selectedAnimal.animalInfo.m_iId){
								public void paintInfo(Graphics g, int x, int y){
									paintIcon(g, x, y);
									String []a = BitmapFont.m_bmNormalFont.splitFontBStrInLine(drugInfo.m_strName, GameCanvas.w-120);
									y += 35 ;
									x += 3;
									BitmapFont.drawNormalFont(g, "Bệnh: ", x, y, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
									int i;										
									for(i =0 ; i< a.length; i ++){						
										BitmapFont.drawBoldFont(g, a[i], x+BitmapFont.m_bmFont.stringWidth("Bệnh: "), y , 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
										y += 15;
									}									
									BitmapFont.drawNormalFont(g, "Bạn đã hết thuốc chữa", x, y + 15, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
									BitmapFont.drawNormalFont(g, "bệnh này", x, y + 30, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
									BitmapFont.drawNormalFont(g, "Bạn có muốn mua thuốc ", x, y + 45, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
									BitmapFont.drawNormalFont(g, "trong cửa hàng không?", x, y + 60, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
									
								}
							};
							
							cL = new Command(GameResource.buy, new IAction() {
								public void perform(){
									InfoScr.gI().close();
									goShopping(2, 0);
								}
							});
						}
						else
						{
							
							//show sick info
							myAnimal = new Animal(m_selectedAnimal.animalInfo.m_iId){
								public void paintInfo(Graphics g, int x, int y){
									paintIcon(g, x, y);
									String []a = BitmapFont.m_bmNormalFont.splitFontBStrInLine(drugInfo.m_strName, 80);
									y += 35;
									x += 3;
									BitmapFont.drawNormalFont(g, "Bệnh: ", x, y, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
									int i;							
									int tmp = BitmapFont.m_bmFont.stringWidth("Bệnh: ");
									for(i =0 ; i< a.length; i ++){						
										BitmapFont.drawBoldFont(g, a[i], x + tmp, y , 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
										y += 15;
									}	
									BitmapFont.drawNormalFont(g, "Bạn hiện có ", x, y + 15, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
									BitmapFont.drawBoldFont(g, ""+numItem, x+BitmapFont.m_bmFont.stringWidth("Bạn hiện có "), y + 15, 0xff0000, Graphics.LEFT | Graphics.VCENTER);
									BitmapFont.drawNormalFont(g, " thuốc", x+BitmapFont.m_bmFont.stringWidth("Bạn hiện có     "), y + 15, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
									
									BitmapFont.drawNormalFont(g, "chữa bệnh này", x, y + 30, 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
									
								}
							};
							
							cL = new Command("Chữa bệnh", new IAction() {
								public void perform(){
									GlobalService.gI().goToDoctor(m_idFarmOwner, myAnimal.animalInfo.m_iId, myAnimal.m_iId, drugInfo.m_iId);
									InfoScr.gI().close();
								}
							});
						}
												
						//gan tat ca cac thuoc tinh cho myAnimal
						m_selectedAnimal.copyTo(myAnimal);
						//....
						
						cR = new Command(GameResource.exit, new IAction() {
							public void perform(){
								InfoScr.gI().close();
							}
						});
												
						InfoScr.gI().switchToMe("Loại bệnh", 160, 180, myAnimal, cC, cL, cR);	
					}
					else{
						//vat nuoi khong bi benh => show msg
						GameCanvas.startOKDlg("Hiện tại vật nuôi không bị bệnh");
					}		           
				}
				exitPetMenuCmd.action.perform();
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(9, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	sellPetCmd = new Command("Bán", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub	
				if(m_selectedAnimal == null)
					return;
	         	
        		GlobalService.gI().sellPet(0, NuoiThuMidlet.m_myPlayerInfo.m_iId, m_selectedAnimal.animalInfo.m_iId, m_selectedAnimal.m_iId);
        		
        		exitPetMenuCmd.action.perform();
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(10, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	harvestCmd = new Command(GameResource.harvest, new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				if(m_selectedAnimal == null)
					return;
				
				GlobalService.gI().harvestMoney(m_idFarmOwner, m_selectedAnimal.animalInfo.m_iId, m_selectedAnimal.m_iId);
				m_selectedAnimal.m_bIsHaveMoney = false;
        		exitPetMenuCmd.action.perform();
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(18, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	//init mini game command
    	songDauCmd = new Command("Song Đấu", new IAction() {
			public void perform() {
				// TODO Auto-generated method stub
				showAnimalListScreen(SERVICEID_GAME_SONG_DAU);
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x - 15, y - 14);
    			GameResource.instance.m_frameIconItems.drawFrame(11, x - 10, y - 10, Sprite.TRANS_NONE, g);
    			//g.drawImage(GameResource.instance.imgQuestion, x-5, y - 5, 0);
        	}
    	};
    	    	
    	    	/*
    	duaThuCmd = new Command("New game", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				GameCanvas.startOKDlg("Game đang được xây dựng, mời bạn trở lại sau");
				
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			//GameResource.instance.m_frameIconItems.drawFrame(12, x - 10, y - 10, Sprite.TRANS_NONE, g);
    			g.drawImage(GameResource.instance.imgQuestion, x-5, y - 5, 0);
        	}
    	};
    	*/
    	
    	hocTiengAnhCmd = new Command("Học tiếng anh", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				showAnimalListScreen(SERVICEID_GAME_HOC_TIENG_ANH);
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(13, x - 10, y - 10, Sprite.TRANS_NONE, g);
    			//g.drawImage(GameResource.instance.imgQuestion, x-5, y - 5, 0);
        	}
    	};
    	quaySoCmd = new Command("Quay số", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				//GameCanvas.startOKDlg("Game đang được xây dựng, mời bạn trở lại sau");
				
				GameCanvas.startWaitDlg(GameResource.plzWait);
				//GlobalService.gI().setHandler(SERVICEID_GAME_QUAY_SO);
				GlobalService.gI().requestPriceQuaySo();
				//showAnimalListScreen(SERVICEID_GAME_QUAY_SO);
				//m_bIsHaveQuaySo = true;
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(14, x - 10, y - 10, Sprite.TRANS_NONE, g);
    			//g.drawImage(GameResource.instance.imgQuestion, x-5, y - 5, 0);
        	}
    	};

    	bauCuaCmd = new Command("Bầu cua", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				
				GameCanvas.startWaitDlg(GameResource.plzWait);
				GlobalService.gI().setHandler(SERVICEID_GAME_BAU_CUA);
				
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(15, x - 10, y - 10, Sprite.TRANS_NONE, g);
    			//g.drawImage(GameResource.instance.imgQuestion, x-5, y - 5, 0);
        	}
    	};
    	doanThuCmd = new Command("Đoán Thú", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				GameCanvas.startWaitDlg(GameResource.plzWait);
				GlobalService.gI().setHandler(SERVICEID_GAME_DOAN_THU);
				
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			GameResource.instance.m_frameIconItems.drawFrame(16, x - 10, y - 10, Sprite.TRANS_NONE, g);
        	}
    	};
    	
    	/*
    	timCapCmd = new Command("New game", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				//GameCanvas.startOKDlg("Game đang được xây dựng, mời bạn trở lại sau");
			}
		}){
    		public void paint(Graphics g, int x, int y) {
    			PaintPopup.drawCircle(g, x-15, y-14);
    			//GameResource.instance.m_frameIconItems.drawFrame(17, x - 10, y - 10, Sprite.TRANS_NONE, g);
    			g.drawImage(GameResource.instance.imgQuestion, x-5, y - 5, 0);
        	}
    	};
    	*/
    	
    	starQuaySoCmd = new Command("Quay", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				GlobalService.gI().doQuaySo(m_sGoldQuaySo);
				m_bIsStarQuay = true;
				m_cmdCenter = null;
				m_cmdRight = null;
			}
		});
    	
    	nextQuaySoCmd = new Command("Tiếp tục", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				m_cmdRight = null;
				m_cmdCenter = null;
				GameCanvas.startWaitDlg(GameResource.plzWait);
				GlobalService.gI().requestPriceQuaySo();
			}
		});
    	
    	exitQuaySocmd = new Command(GameResource.no, new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				m_bIsHaveQuaySo = false;
				m_cmdRight = null;
				m_cmdCenter = mainControlCmd;
			}
		});
    }
    
    public void showChoose2Animal() {
		// TODO Auto-generated method stub
    	
    	if(m_arrSelectAnimal.size() >= 2){
    	
			Command right  = new Command(GameResource.close, new IAction() {
				
				public void perform() {
					// TODO Auto-generated method stub
					AnimalListSrc.gI().close();
				}
			});
						
			orderAnimalById();
			
			AnimalListSrc.gI().setListItem(m_arrSelectAnimal);
			AnimalListSrc.gI().switchToMe("Chọn thú", 160, 160, null, null, right);
    	}
    	else{
    		GameCanvas.startOKDlg("Bạn phải có ít nhất 2 con thú cùng loài để lai thú!");
    	}
	}

	public void doUseItemInStock(){
        final MyObj obj = ShopScr.gI().getSelectedItem();       
        final Item item = (Item) obj;
        if (item == null) {
            return;
        }
    
        Vector vt = null;
        
        if(item.m_bItemType == Item.ITEM_TYPE_AVATAR){        	
        	vt = new Vector();

    		vt.addElement( new Command("Sử dụng", new IAction() {
				public void perform(){
					GlobalService.gI().doChangeAvatar(item.m_iId);
                    GameCanvas.endDlg();					

					ShopScr.gI().m_bIsShowPopupInfo = true;
				}
			}));
    		    		 
        } else if(item.m_bItemType == Item.ITEM_TYPE_DRUG){
        	if(item.m_iId >= 4){
        		vt = new Vector();

        		vt.addElement( new Command("Sử dụng", new IAction() {
					public void perform(){
						ShopScr.gI().close();

						if(item.m_iId == 4){
							//su dung thuoc dot bien
							Command right = new Command(GameResource.close, new IAction() {
	    						public void perform(){
	    							AnimalListSrc.gI().close();
	    						}
	    					});
	    					Command center = new Command(GameResource.select, new IAction() {
	    						public void perform(){
	    							
	    							AnimalListSrc.gI().close();
	    							GameCanvas.startWaitDlg(GameResource.plzWait);
	    							
	    							Animal ani = (Animal)AnimalListSrc.gI().getSelectedItem();
	    					    	GlobalService.gI().goToDoctor(m_idFarmOwner, ani.animalInfo.m_iId, ani.m_iId, item.m_iId);
	    						}
	    					});
	    			
	    					Vector arrNewAnimal = new Vector();
	    					int i;
	    					for(i = 0 ; i < m_arrAnimal.size(); i++){
	    						arrNewAnimal.addElement(m_arrAnimal.elementAt(i));
	    					}				
	    					
	    					AnimalListSrc.gI().setListItem(arrNewAnimal);
	    					AnimalListSrc.gI().switchToMe("Chọn thú", 160, 160, null, center, right);
						}
						else if(item.m_iId == 5){
							//su dung thuoc de lai thu
							showChoose2Animal();
						}
						
						ShopScr.gI().m_bIsShowPopupInfo = true;
					}
				}));
        	}
        }
        else if(item.m_bItemType == Item.ITEM_TYPE_TOY){
        	if(m_idFarmOwner != NuoiThuMidlet.m_myPlayerInfo.m_iId)
        	{
        		return;
        	}
        	        	
        	vt = new Vector();

    		vt.addElement( new Command("Sử dụng", new IAction() {
				public void perform(){
					ShopScr.gI().close();
					
					m_bIsSelectedToy = true;
					m_selectedToy = (Toy)item;
					m_selectedToy.m_bIsFocus = true;
					m_selectedToy.m_iTileX = 1;
					m_selectedToy.m_iTileY = 1;
					
					m_iPreTileX = -1;
					m_iPreTileY = -1;
					
					m_cmdRight = exitToyCmd;
					m_cmdCenter = setPosToyCmd;					

					ShopScr.gI().m_bIsShowPopupInfo = true;
				}
			}));     	
        }
        
        if(vt != null){
        	vt.addElement(new Command(GameResource.close, new IAction() {
				public void perform(){
					ShopScr.gI().m_bIsShowPopupInfo = true;
				}
			}));

    		GameCanvas.menu.startAt(vt, 0);
    		GameCanvas.menu.setCustomPos(ShopScr.gI().m_iFocusX, ShopScr.gI().m_iFocusY);
    			
    		//turn off popup info
    		ShopScr.gI().m_bIsShowPopupInfo = false; 
        }
    }

    public void showAnimalListScreen(final int gameID){
    	Command right = new Command(GameResource.close, new IAction() {
			public void perform(){
				AnimalListSrc.gI().close();
			}
		});
		Command center = new Command(GameResource.select, new IAction() {
			public void perform(){
				AnimalListSrc.gI().close();
				GameCanvas.startWaitDlg(GameResource.plzWait);
				
				Animal ani = (Animal)AnimalListSrc.gI().getSelectedItem();
		    	GlobalService.gI().doSetPetPlayGame(ani.animalInfo.m_iId, ani.m_iId, NuoiThuMidlet.m_myPlayerInfo.m_iId, gameID);
			}
		});
		
		orderAnimalById();
		
		AnimalListSrc.gI().setListItem(m_arrSelectAnimal);
		AnimalListSrc.gI().switchToMe("Chọn thú", 160, 160, null, center, right);
    }
    
    public void doBuyItem() {
        final MyObj obj = ShopScr.gI().getSelectedItem();        
        final ItemInfo item = (ItemInfo) obj;
        if (item == null) {
            return;
        }
    
        int numberItem = ShopScr.gI().m_numberPrice;
        
        //check level require
        if (NuoiThuMidlet.m_myPlayerInfo.getLevel() < item.m_sLevelRequired) {
            GameCanvas.startOKDlg(GameResource.notEnoughLvToDoSth + item.m_strName);
            return;
        }

        if (!ShopScr.gI().isShowPrice() && !(item instanceof AnimalInfo || item instanceof AvatarInfo || item instanceof ToyInfo)) {
        	ShopScr.gI().m_bIsKeyArrow = true;
        	ShopScr.gI().setShowPrice(true);
        	ShopScr.gI().m_numberPrice = 1;
        } else {
        	if(item instanceof AvatarInfo){
        		numberItem = 1;
        		AvatarInfo avaInfo = (AvatarInfo)item;
        		if(!avaInfo.m_bIsSell && avaInfo.m_sTimeExpired > 0){
        			GameCanvas.startOKDlg("Hiện cửa hàng đã hết bán vật phẩm này");
        			return;
        		}
        	}
        	
        	if(item instanceof ToyInfo){
        		numberItem = 1;
        		ToyInfo toyInfo = (ToyInfo)item;
        		if(!toyInfo.m_bIsSell && toyInfo.m_sTimeExpired > 0){
        			GameCanvas.startOKDlg("Hiện cửa hàng đã hết bán vật phẩm này");
        			return;
        		}
        	}
        	
            if (item instanceof AnimalInfo) {
                numberItem = 1;
            }
            
            final int count = numberItem;

            Vector vector = new Vector();
            if (item.m_iMoney[0] > 0) {
                Command cmdXu = new Command(GameResource.gold, new IAction() {

                    public void perform() {
                        // TODO Auto-generated method stub		
                        if (NuoiThuMidlet.m_myPlayerInfo.m_iMoney[0] < item.m_iMoney[0] * count) {
                            GameCanvas.startOKDlg(GameResource.notEnoughMoney);
                            return;
                        }

                        //close shop
                        //ShopScr.gI().close();
                        
                        GlobalService.gI().doBuyItem(item.m_bItemType, item.m_iId, count, 0);

                        GameCanvas.endDlg();
                        ShopScr.gI().setShowPrice(false);
                    }
                });
                vector.addElement(cmdXu);
            }
            if (item.m_iMoney[1] > 0) {
                Command cmdLuong = new Command(GameResource.dina, new IAction() {

                    public void perform() {
                        // TODO Auto-generated method stub						
                        if (NuoiThuMidlet.m_myPlayerInfo.m_iMoney[1] < item.m_iMoney[1] * count) {
                            GameCanvas.startOKDlg(GameResource.notEnoughMoney);
                            return;
                        }

                      //close shop
                        //ShopScr.gI().close();
                        
                        GlobalService.gI().doBuyItem(item.m_bItemType, item.m_iId, count, 1);

                        GameCanvas.endDlg();
                        ShopScr.gI().setShowPrice(false);
                    }
                });
                vector.addElement(cmdLuong);
            }
            Command cmdClose = new Command(GameResource.close, new IAction() {

                public void perform() {
                    // TODO Auto-generated method stub
                    GameCanvas.endDlg();
                    ShopScr.gI().setShowPrice(false);
                    
                }
            });
            vector.addElement(cmdClose);

            GameCanvas.startOKDlg(GameResource.buyItem, vector);
        }
    }
    
    private void updateChatAnimal(){
    	int a = m_arrAnimal.size();
    	if(a>0){
	    	m_iTimeChat -- ;
	    	if(m_iTimeChat < 0 ){
	    		int count = CRes.random(0, m_arrAnimal.size());
	    		Animal ani = (Animal) m_arrAnimal.elementAt(count);
	    		ani.chat(m_arrChatContent[CRes.random(0, m_arrChatContent.length)]);
	    			    		
	    		m_iTimeChat = 1000;
	    	}
    	}
    }
    
    public void update() {
        // TODO Auto-generated method stub
    	//update chat
    	updateChatAnimal();
        //update animal
        int a = m_arrAnimal.size();
        for (int i = 0; i < a; i++) {
            Animal ani = (Animal) m_arrAnimal.elementAt(i);
            //ani.update();
            ani.update();
            if(ani.m_bActionBath){
        		int tmpY = TileMap.m_iHeightTile * TileMap.instance.m_gardenMap.m_iMapHeight;
                tmpY = GameCanvas.h - tmpY*2/3 + 10;
        		//water animation
        		FallingStar fall = new FallingStar(); 
        		fall.startEffect(GameCanvas.w - 55, tmpY - 10, 3, 5);
        		ani.m_bActionBath = false;
    		}
            if(ani.m_bCompleteAction){
            	if(1==ani.m_iGoToAction){
            		GlobalService.gI().feedAnimal(m_idFarmOwner, ani.animalInfo.m_iId, ani.m_iId, ani.m_iFeedFood);
            	}
	        	else if (2==ani.m_iGoToAction){	  
	        		GlobalService.gI().takeABathPet(m_idFarmOwner, ani.animalInfo.m_iId, ani.m_iId);
	        	}
	        	ani.m_bCompleteAction = false;
	        	ani.m_iGoToAction = 0;
            }
        }
        if(a > 0)
        	m_arrAnimal = orderAnimal(m_arrAnimal);

        //update my player
        NuoiThuMidlet.m_myPlayerInfo.update();
        
        // update quay so
        updateQuaySo();
    }

    private void paintObj(Graphics g){
		int animalIndex = 0;
    	int toyIndex = 0;
    	for(animalIndex = 0; animalIndex < m_arrAnimal.size(); animalIndex++){
    		Animal ani = (Animal)m_arrAnimal.elementAt(animalIndex);
    		if(ani.m_iGoToAction == 1){
            	int xfeeder = 5;
        		int yfeeder = GameCanvas.h - GameCanvas.hBottomBar - GameResource.instance.imgFeeder.getHeight() - 50;
        		g.drawImage(GameResource.instance.imgFood, xfeeder+3, yfeeder+6, Graphics.LEFT | Graphics.TOP);
        		break;
            }
			
    	}
    	animalIndex = 0;
    	while(animalIndex < m_arrAnimal.size() && toyIndex < m_arrToy.size()){
    		Animal ani = (Animal)m_arrAnimal.elementAt(animalIndex);
    		Toy toy = (Toy)m_arrToy.elementAt(toyIndex);
    		if(ani.m_position.y < toy.m_Y + toy.itemInfo.m_imgPart.m_iH){
    			ani.paint(g);
    			animalIndex++;
    		}
    		else
    		{
    			toy.paint(g);
    			toyIndex++;
    		}
    	}
    	for( ; animalIndex < m_arrAnimal.size(); animalIndex++){
    		 Animal ani = (Animal) m_arrAnimal.elementAt(animalIndex);     
             ani.paint(g);
    	}
    	
    	for( ; toyIndex < m_arrToy.size(); toyIndex++){
   		 	Toy toy = (Toy) m_arrToy.elementAt(toyIndex);      
   		 	toy.paint(g);
    	}
    }

    public void paintGate(Graphics g, int x, int y){
		int maxW = GameCanvas.hw >> 1;
		while(x < maxW){
			g.drawImage(GameResource.instance.imgFance, x, y, Graphics.LEFT | Graphics.TOP);
			x += GameResource.instance.imgFance.getWidth();	
		}
		
		g.drawImage(GameResource.instance.imgGate, x-3, y + GameResource.instance.imgFance.getWidth(), Graphics.LEFT | Graphics.BOTTOM);		

		//mark name in gate
		m_iPosXName = x + (GameResource.instance.imgGate.getWidth() >> 1);
		
		x += GameResource.instance.imgGate.getWidth() - 6;
		while(x < GameCanvas.w){
			g.drawImage(GameResource.instance.imgFance, x, y, Graphics.LEFT | Graphics.TOP);
			x += GameResource.instance.imgFance.getWidth();
		}
    }
    
    public void paint(Graphics g) {
        g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
        g.translate(-g.getTranslateX(), -g.getTranslateY());
        
        g.drawImage(m_imgBackground, 0, 0, 0);
        
        int tmpY = TileMap.m_iHeightTile * TileMap.instance.m_gardenMap.m_iMapHeight;
        tmpY = GameCanvas.h - tmpY*2/3 + 10;
        
        paintObj(g);
        if(m_selectedToy != null){
        	m_selectedToy.paint(g);
        }
        
        if (m_friend != null) {
            m_friend.paintInTopBar(g, GameCanvas.w2d3 - 20, 2);
        
            //paint name in the gate
    		BitmapFont.drawBoldFont(g, m_friend.m_strName, m_iPosXName, tmpY - (GameResource.instance.imgGate.getHeight()>>1)+5, 0xfffefe, Graphics.VCENTER|Graphics.HCENTER);
       } else {
            NuoiThuMidlet.m_myPlayerInfo.paintInTopBar(g, 2, 2);
            
            //paint name in the gate
    		BitmapFont.drawBoldFont(g, NuoiThuMidlet.m_myPlayerInfo.m_strName, m_iPosXName, tmpY - (GameResource.instance.imgGate.getHeight()>>1)+5, 0xfffefe, Graphics.VCENTER|Graphics.HCENTER);
        }
        
		
		
        if (m_nMsg > 0) {
            BitmapFont.drawOutlinedString(g, m_nMsg + " ", GameCanvas.w - 20, 25, 0xffff00, 0x000000, Graphics.RIGHT | Graphics.VCENTER);
            g.drawImage(GameResource.instance.imgMesssage, GameCanvas.w - 20, 20, 0);
        }

        if (m_bIsOpenScrAnimation) {
            g.setColor(1);
            g.fillRect(0, 0, GameCanvas.w, GameCanvas.hh - countDown);
            g.fillRect(0, GameCanvas.hh + countDown, GameCanvas.w, GameCanvas.hh - countDown);

            countDown += 15;
            if (countDown >= GameCanvas.hh) {
                countDown = 0;
                m_bIsOpenScrAnimation = false;
            }
        }
        
        //paint quay so
        drawQuaySo(g);
        
      
        super.paint(g);
    }
    
    public void updateKey() {
        if (GameCanvas.isPointerClick) {            
        	if(m_bIsSelectedPet)
        	{
        		int i, xAni;
        		for(i = 0; i < m_arrAnimal.size(); i++){
        			//Animal ani = (Animal)m_arrAnimal.elementAt(i);
        			Animal ani = (Animal)m_arrAnimal.elementAt(i);
        			if(ani.m_position.anchor == 1){//right
        				xAni = ani.m_position.x - ani.animalInfo.m_iW[1];
        			}
        			else
        			{
        				xAni = ani.m_position.x;
        			}
        			if(GameCanvas.isPointer(xAni, ani.m_position.y - ani.animalInfo.m_iH[1], ani.animalInfo.m_iW[1], ani.animalInfo.m_iH[1])){
        				if(ani.m_bIsFocus){
        					//neu da chon roi => mo menu con vat ra
        					mainControlCmd.action.perform();
        				}
        				else
        				{
        					int index = m_arrAnimal.indexOf(ani);
        					Animal aniTmp = (Animal)m_arrAnimal.elementAt(index);
        					//doi focus
        					m_selectedAnimal.m_bIsFocus = false;
            				
            				m_selectedAnimal = aniTmp;
            				aniTmp.m_bIsFocus = true;
            				break;
        				}
        				break;
        			}
        		}
        	}
        	else if(m_bIsSelectedToy){
        		int i, j, x, y;
        		int limitX = GameCanvas.w / TileMap.m_iWidthTile;
        		
        		for(i = 1; i <= 8; i++ ){
        			for(j = 1; j <= limitX; j++){
        				x = j * TileMap.m_iWidthTile;
                		y = (GameCanvas.h - GameCanvas.hBottomBar) - i * TileMap.m_iHeightTile;
                		if(GameCanvas.isPointer(x, y, TileMap.m_iWidthTile, TileMap.m_iHeightTile)){
                			if(m_selectedToy.m_iTileX == j && m_selectedToy.m_iTileY == i){
                				//chon
                				m_cmdCenter.action.perform();
                			}
                			else
                			{
                				//doi focus
                				m_selectedToy.m_iTileX = (short)j;
                				m_selectedToy.m_iTileY = (short)i;
                			}
                			return;
                		}                				
        			}
        		}        		
        	}
        	else if(m_bIsFocusToy){
        		for(int toyIndex = 0 ; toyIndex < m_arrToy.size(); toyIndex++){
           		 	Toy toy = (Toy) m_arrToy.elementAt(toyIndex);      
	           		int x = toy.m_iTileX * TileMap.m_iWidthTile;
	         		int y = (GameCanvas.h - GameCanvas.hBottomBar) - toy.m_iTileY * TileMap.m_iHeightTile;
	         		if(GameCanvas.isPointer(x, y, TileMap.m_iWidthTile, TileMap.m_iHeightTile)){
	         			m_selectedToy.m_bIsFocus = false;
	         			m_selectedToy = toy;
	         			m_selectedToy.m_bIsFocus = true;
	         			return;
	         		}
            	}
        	}
        	
        }
        
        if (GameCanvas.keyPressed[8]) {
        	if(m_bIsSelectedToy){
        		m_selectedToy.m_iTileY--;
        		if(m_selectedToy.m_iTileY < 1){
        			m_selectedToy.m_iTileY = 1;
        		}
        	}
        	
        	GameCanvas.keyPressed[8] = false;

        } else if (GameCanvas.keyPressed[2]) {
        	if(m_bIsSelectedToy){
        		m_selectedToy.m_iTileY++;
        		if(m_selectedToy.m_iTileY > 8){
        			m_selectedToy.m_iTileY = 8;
        		}
        	}
        	
            GameCanvas.keyPressed[2] = false;

        } else if (GameCanvas.keyPressed[4]) {
        	if(m_bIsSelectedToy){
        		m_selectedToy.m_iTileX--;
        		if(m_selectedToy.m_iTileX < 1){
        			m_selectedToy.m_iTileX = 1;
        		}
        	}else if(m_bIsFocusToy){
        		//change focus toy
        		m_selectedToy.m_bIsFocus = false;
        		int index = m_arrToy.indexOf(m_selectedToy);
        		index--;
        		if(index < 0)
        			index = m_arrToy.size() - 1;
        		m_selectedToy = (Toy)m_arrToy.elementAt(index);
        		m_selectedToy.m_bIsFocus = true;
        	}
        	else if(m_bIsSelectedPet && m_arrAnimal.size() > 1)
            {
        		m_selectedAnimal.m_bIsFocus = false;
        		int index = m_arrSelectAnimal.indexOf(m_selectedAnimal);
        		index--;
        		if(index < 0)
        			index = m_arrSelectAnimal.size() - 1;
        		Animal ani = (Animal)m_arrSelectAnimal.elementAt(index);
        		////trỏ đúng vị trí con thú trong mảng động
        		index = m_arrAnimal.indexOf(ani);
				
        		m_selectedAnimal = (Animal)m_arrAnimal.elementAt(index);
        		m_selectedAnimal.m_bIsFocus = true;
        	}
        	
        	GameCanvas.keyPressed[4] = false;

        } else if (GameCanvas.keyPressed[6]) {
        	if(m_bIsSelectedToy){
        		m_selectedToy.m_iTileX++;
        		if(m_selectedToy.m_iTileX * TileMap.m_iWidthTile > (GameCanvas.w - TileMap.m_iWidthTile)){
        			m_selectedToy.m_iTileX--;
        		}
        	}else if(m_bIsFocusToy){
        		//change focus toy
        		m_selectedToy.m_bIsFocus = false;
        		int index = m_arrToy.indexOf(m_selectedToy);
        		index++;
        		if(index >= m_arrToy.size())
        			index = 0;
        		m_selectedToy = (Toy)m_arrToy.elementAt(index);
        		m_selectedToy.m_bIsFocus = true;
        	}
        	else if(m_bIsSelectedPet && m_arrAnimal.size() > 1)
            {
        		m_selectedAnimal.m_bIsFocus = false;
        		int index = m_arrSelectAnimal.indexOf(m_selectedAnimal);
        		index++;
        		if(index >= m_arrAnimal.size())
        			index = 0;
        		Animal ani = (Animal)m_arrSelectAnimal.elementAt(index);
        		
        		index = m_arrAnimal.indexOf(ani);
				
        		m_selectedAnimal = (Animal)m_arrAnimal.elementAt(index);
        		m_selectedAnimal.m_bIsFocus = true;
        	}
        	GameCanvas.keyPressed[6] = false;
        }
        
        super.updateKey();
    }
    
    public void paintMarqueeMsg(Graphics g){
    	g.setClip(70, 20, GameCanvas.w - 70, 18);
		PaintPopup.paintBox(g, 70, 18, GameCanvas.w - 70, 32, 1, 0x000000, 0x000000);
    	BitmapFont.drawBoldFont(g, m_strMarqueeMsg, m_iPosXMsg, 30, 0xffffff, Graphics.LEFT | Graphics.VCENTER);
		m_iTimeDisplay++;
		m_iPosXMsg-- ;
		if(m_iPosXMsg <= - m_iStrWidth){
			m_iPosXMsg = GameCanvas.w;
		}
		if(m_iTimeDisplay > 1000){
			m_iTimeDisplay = 0;
			m_strMarqueeMsg = null;
		}
	}

    ///////////////////////////////////////////////// SERVER RESPONSE ///////////////////////////////////////////////	
    public void onBuyItem(byte itemType, short itemID, short numItem,
            byte typeMoney, int moneyBuy, int exp) {
        // TODO Auto-generated method stub
        NuoiThuMidlet.m_myPlayerInfo.subMoney(moneyBuy, typeMoney);
        NuoiThuMidlet.m_myPlayerInfo.m_sExperience += exp;
        
        if(itemType == Item.ITEM_TYPE_AVATAR){
        	Avatar ava = new Avatar(itemID);
        	ava.m_iNumItem = 1;
        	ava.m_iId = itemID;
        	DataManager.gI().m_myLstAvatar.addElement(ava);
        }else if(itemType != Item.ITEM_TYPE_ANIMAL){
        	DataManager.gI().addItemToContainer(itemType, itemID, numItem);
        }
         
        /*
        FlyText flyExp = new FlyText();
        flyExp.startEffect("+" + exp, 20, 20, 0x00ff00, 0x000000);
        
        FlyText flyMoney = new FlyText();
        flyMoney.startEffect("-" + moneyBuy, 30, 30, 0xffff00, 0x000000);
        */
    }
    
    public void onFriendList(Vector vtFriends) {
        // TODO Auto-generated method stub
        //cache data
    	DataManager.gI().m_myListFriend = vtFriends;
    	
        ListScr.gI().setListItem(vtFriends);

        Command chatCmd = null;
        Command selectedCmd = null;
        if(vtFriends.size() > 0){
        	chatCmd = new Command(GameResource.chat, new IAction() {

        		public void perform() {
                // TODO Auto-generated method stub
        			ListScr.gI().close();
        			MsgScreen.gI().flag = false;
	                PlayerInfo p = (PlayerInfo) ListScr.gI().getSelectedItem();
	                if (p != null) {
	                    MsgScreen.gI().startChatTo(p.m_iId, p.m_strName);
	                    MsgScreen.gI().switchToMe(GameCanvas.currentScreen);
	                }
	                m_nMsg = 0;
	
	            }
	        });

	        selectedCmd = new Command("Thăm nhà", new IAction() {
	            public void perform() {
	                // TODO Auto-generated method stub
	                PlayerInfo p = (PlayerInfo) ListScr.gI().getSelectedItem();
	                if (p != null) {
	                    if (p.m_iId != NuoiThuMidlet.m_myPlayerInfo.m_iId) {
	                        GameCanvas.startWaitDlg(GameResource.plzWait);
	
	                        GlobalService.gI().getGardenData(p.m_iId);
	
	                        GameCanvas.mainScr.m_friend = p;
	                    }
	                } else {
	                    GameCanvas.startOKDlg(GameResource.emptyFriendLst);
	                }
	            }
	        });
        }

        Command menuFriendCmd = new Command(GameResource.menu, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                Vector menu = new Vector();
                if(DataManager.gI().m_myListFriend.size() > 0){
	                Command delFriendCmd = new Command(GameResource.del, new IAction() {
	
	                    public void perform() {
	                        // TODO Auto-generated method stub
	                    	
	                        if (ListScr.gI().getItemSize() > 0) {
	                            PlayerInfo p = (PlayerInfo) ListScr.gI().getSelectedItem();
	
	                            GlobalService.gI().doDelFriend(p.m_iId);
	                            ListScr.gI().removeItem(p);
	
	                            //remove friend
	                            DataManager.gI().m_myListFriend.removeElement(p);
	                        } else {
	                            GameCanvas.startOKDlg(GameResource.emptyFriendLst);
	                        }
	                    }
	                });
	                menu.addElement(delFriendCmd);
	
	                Command viewFriendInfoCmd = new Command(GameResource.viewInfo, new IAction() {
	
	                    public void perform() {
	                        // TODO Auto-generated method stub
	                        //close friend list screen
	                    	ListScr.gI().close();
	                        
	                    	//show info screen
	                        PlayerInfo p = (PlayerInfo) ListScr.gI().getSelectedItem();
	                        Command close = new Command(GameResource.close, new IAction() {
								
								public void perform() {
									// TODO Auto-generated method stub
									InfoScr.gI().close();
								}
							});
	                        InfoScr.gI().switchToMe(GameResource.viewInfo, 160, 180, p, null, null, close);
	                    }
	                });
	                menu.addElement(viewFriendInfoCmd);
                }

                Command addFriendCmd = new Command(GameResource.search, new IAction() {

                    public void perform() {
                        // TODO Auto-generated method stub
                        ListScr.gI().close();
                        SearchFriendScr.gI().switchToMe();
                    }
                });
                menu.addElement(addFriendCmd);
                Command cmdClose = new Command(GameResource.close, new IAction() {

                    public void perform() {
                        if (MainScr.gI().selectedRankIndex != -1) {
                            MainScr.gI().selectedRankIndex = -1;
                        }
                        ListScr.gI().close();
                    }
                });
                menu.addElement(cmdClose);
                GameCanvas.menu.startAt(menu, 0);
            }
        });

        ListScr.gI().switchToMe(GameResource.friendlist, menuFriendCmd, chatCmd, selectedCmd);
        GameCanvas.endDlg();
    }
            
    public void onChatFrom(int playerID, String playerName, String content) {
        // TODO Auto-generated method stub
        if (GameCanvas.currentScreen != MsgScreen.gI()) {
            m_nMsg++;
        } else {
            m_nMsg = 0;
        }

        MsgScreen.gI().onChatFrom(playerID, playerName, content);
    }

    public void onSearchPlayer(Vector listPlayer) {
        // TODO Auto-generated method stub
        if (listPlayer.size() > 0) {
            CRes.cleanVector(DataManager.gI().m_vtSearchFriend);
            DataManager.gI().m_vtSearchFriend = listPlayer;

            ListScr.gI().setListItem(listPlayer);
            Command addFriendCmd = new Command(GameResource.addFreind, new IAction() {

                public void perform() {
                    // TODO Auto-generated method stub
                    PlayerInfo p = (PlayerInfo) ListScr.gI().getSelectedItem();
                    if (p.m_iId == NuoiThuMidlet.m_myPlayerInfo.m_iId) {
                        GameCanvas.startOKDlg("Không thể thêm bạn");
                        return;
                    }

                    GlobalService.gI().doRequestAddFriend(p.m_iId);
                    GameCanvas.startOKDlg(GameResource.requestFriendSent + p.m_strName);
                }
            });

            ListScr.gI().switchToMe(GameResource.search, null, addFriendCmd, null);
            GameCanvas.endDlg();
        } else {
            GameCanvas.startOKDlg(GameResource.friendNotFound);
            listPlayer = null;
        }
    }
    
    public void onListSms(Vector vtSms) {
        // TODO Auto-generated method stub
        DataManager.gI().m_vtSMS = vtSms;

        ListScr.gI().setListItem(vtSms);
        Command napTienCmd = new Command(GameResource.select, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                MoneyInfo mInfo = (MoneyInfo) ListScr.gI().getSelectedItem();
                if (mInfo != null) {
                    NuoiThuMidlet.sendSMS(mInfo.smsContent + NuoiThuMidlet.m_myPlayerInfo.m_strName, mInfo.smsTo, new IAction() {

                        public void perform() {
                            // TODO Auto-generated method stub
                            GameCanvas.startOKDlg(GameResource.smsSent);
                        }
                    }, new IAction() {

                        public void perform() {
                            // TODO Auto-generated method stub
                            GameCanvas.startOKDlg(GameResource.sendSMSFail);
                        }
                    });
                }
            }
        });
        GameCanvas.endDlg();
        ListScr.gI().switchToMe(GameResource.chargeMoney, null, napTienCmd, null);
    }
    
    public void onListDiary(Vector vDiary) {
        // TODO Auto-generated method stub
        GameCanvas.endDlg();

        CRes.cleanVector(DataManager.gI().m_vtDiary);
        DataManager.gI().m_vtDiary = vDiary;

        ListScr.gI().setListItem(vDiary);
        ListScr.gI().switchToMe(GameResource.diary, null, null, null);
    }
    
    public void onListTopPlayers(Vector listTopPlayers) {
        // TODO Auto-generated method stub
        //cache data
    	/*
        DataManager.gI().m_ListHighLevel = null;
        DataManager.gI().m_ListHighGold = null;
        DataManager.gI().m_ListHighDina = null;
        if (selectedRankIndex == 0) {
        	DataManager.gI().m_ListHighLevel = listTopPlayers;
        } else if (selectedRankIndex == 1) {
        	DataManager.gI().m_ListHighGold = listTopPlayers;
        } else if (selectedRankIndex == 2) {
        	DataManager.gI().m_ListHighDina = listTopPlayers;
        }
        */
    	
        ListScr.gI().setListItem(listTopPlayers);


        Command chatCmd = new Command(GameResource.chat, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                ListScr.gI().close();

                PlayerInfo p = (PlayerInfo) ListScr.gI().getSelectedItem();
                if (p != null) {
                    MsgScreen.gI().switchToMe(GameCanvas.currentScreen);
                    MsgScreen.gI().startChatTo(p.m_iId, p.m_strName);
                }
                m_nMsg = 0;

            }
        });

        Command viewInfoCmd = new Command(GameResource.viewInfo, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub

                if (ListScr.gI().getItemSize() > 0) {
                    ListScr.gI().close();
//                    MsgScreen.gI().flag = true;
                    PlayerInfo p = (PlayerInfo) ListScr.gI().getSelectedItem();
                    if (p != null) {
//                    MsgScreen.gI().switchToMe(GameCanvas.currentScreen);
//                    MsgScreen.gI().profileScr(p);
                        Command close = new Command(GameResource.close, new IAction() {
							
							public void perform() {
								// TODO Auto-generated method stub
								InfoScr.gI().close();
							}
						});
                    	InfoScr.gI().switchToMe(GameResource.viewInfo, 160, 180, p, null, null, close);
                    } else {
                        GameCanvas.startOKDlg(GameResource.emptyInfo);
                    }
                }
            }
        });
        
        Command menuRankCmd = new Command(GameResource.close, new IAction() {
            public void perform() {
                if (MainScr.gI().selectedRankIndex != -1) {
                    MainScr.gI().selectedRankIndex = -1;
                }
                ListScr.gI().close();
            }
        });
        
        /*
        Command menuRankCmd = new Command(GameResource.otherRank, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                Vector menu = new Vector();

                Command highGold = new Command(GameResource.highGold, new IAction() {

                    public void perform() {
                        if (selectedRankIndex != 1) {
                            if (GardenData.gI().m_ListHighGold == null) {
                                //request list
                                ListScr.gI().close();
                                GameCanvas.startWaitDlg(GameResource.plzWait);
                                selectedRankIndex = 1;
                                GlobalService.gI().doViewTopPlayer(selectedRankIndex);
                            } else {
                                //show list
                                onListTopPlayers(GardenData.gI().m_ListHighGold);
                            }
                        }
                    }
                });
                menu.addElement(highGold);

                Command highDina = new Command(GameResource.highDina, new IAction() {

                    public void perform() {
                        if (selectedRankIndex != 2) {
                            if (GardenData.gI().m_ListHighDina == null) {
                                ListScr.gI().close();
                                //request list
                                GameCanvas.startWaitDlg(GameResource.plzWait);
                                selectedRankIndex = 2;
                                GlobalService.gI().doViewTopPlayer(selectedRankIndex);
                            } else {
                                //show list
                                onListTopPlayers(GardenData.gI().m_ListHighDina);
                            }
                        }
                    }
                });
                menu.addElement(highDina);

                Command highLevel = new Command(GameResource.highLevel, new IAction() {

                    public void perform() {
                        if (selectedRankIndex != 0) {
                            if (GardenData.gI().m_ListHighLevel == null) {
                                ListScr.gI().close();
                                //request list
                                GameCanvas.startWaitDlg(GameResource.plzWait);
                                selectedRankIndex = 0;
                                GlobalService.gI().doViewTopPlayer(selectedRankIndex);
                            } else {
                                //show list
                                onListTopPlayers(GardenData.gI().m_ListHighLevel);
                            }
                        }
                    }
                });
                Command cmdClose = new Command(GameResource.close, new IAction() {

                    public void perform() {
                        if (MainScr.gI().selectedRankIndex != -1) {
                            MainScr.gI().selectedRankIndex = -1;
                        }
                        ListScr.gI().close();
                    }
                });
                menu.addElement(highLevel);
                menu.addElement(cmdClose);
                GameCanvas.menu.startAt(menu, 0);
            }
        });
        */
        
        if (selectedRankIndex == 0) {
            ListScr.gI().switchToMe(GameResource.highLevel, menuRankCmd, chatCmd, viewInfoCmd);
        } else if (selectedRankIndex == 1) {
            ListScr.gI().switchToMe(GameResource.highGold, menuRankCmd, chatCmd, viewInfoCmd);
        } else if (selectedRankIndex == 2) {
            ListScr.gI().switchToMe(GameResource.highDina, menuRankCmd, chatCmd, viewInfoCmd);
        }
        GameCanvas.endDlg();
        //System.gc();
    }

    

    public void onRequestListTask(short questNumber, Vector listTask,
            short bonusMoney1, short bonusMoney2, short bonusXp, Vector vtItems) {
        // TODO Auto-generated method stub
        ListQuestScr.gI().switchToMe(GameResource.task + GameResource.space + questNumber, listTask, vtItems, bonusMoney1, bonusMoney2, bonusXp, null, null);
        GameCanvas.endDlg();
    }

    public void onQuestCompleted(short questNumber, short bonusMoney1,
            short bonusMoney2, short bonusXp, Vector vtItems, final int level) {
        // TODO Auto-generated method stub

        final short bMoney1 = bonusMoney1;
        final short bMoney2 = bonusMoney2;
        final short bXp = bonusXp;
        int a = vtItems.size();
        for (int i = 0; i < a; i++) {
            Item item = (Item) vtItems.elementAt(i);
            DataManager.gI().addItemToContainer(item.m_bItemType, (short)item.m_iId, (short)item.m_iNumItem);
        }
        
        GameCanvas.showQuestCompletedDlg(questNumber, bonusMoney1, bonusMoney2, bonusXp, vtItems, new Command(GameResource.OK, new IAction() {
            public void perform() {
                // TODO Auto-generated method stub
                GameCanvas.endDlg();
                
                if(bMoney1 > 0){
                	NuoiThuMidlet.m_myPlayerInfo.addMoney(bMoney1, 0);               	
        			
        	        FlyText flyMoney = new FlyText();
        	        flyMoney.startEffect("+" + bMoney1, 30, 30, 0xffff00, 0x000000);
                }
                
                if(bMoney2 > 0){
                	NuoiThuMidlet.m_myPlayerInfo.addMoney(bMoney2, 1);
                	
                	FlyText flyMoney = new FlyText();
           	        flyMoney.startEffect("+" + bMoney2, 30, 30, 0xffff00, 0x000000);
                }
                
                NuoiThuMidlet.m_myPlayerInfo.m_sExperience += bXp;
                if(bXp > 0){
                	FlyText flyExp = new FlyText();
        	        flyExp.startEffect("+" + bXp, 20, 20, 0x00ff00, 0x000000);
        	    }
                
                /*
                if (NuoiThuMidlet.m_myPlayerInfo.checkLevelUp(level)) {
                    GameCanvas.startOKDlg(GameResource.levelUpSuccess + GameResource.space + SkyGardenMidlet.m_myPlayerInfo.getLevel());
                }
                */
            }
        }));
    }
    
	public void onBuyAnimal(byte itemType, short itemID, short animalID,
			byte gender, byte typeMoney, int moneyBuy, int exp) {
		// TODO Auto-generated method stub
		if(itemType == Item.ITEM_TYPE_ANIMAL){
			Animal a = new Animal(itemID);
			a.m_iId = animalID;
			a.m_strName = a.animalInfo.m_strName + " " + a.m_iId;
			a.m_iGender = gender;		
			a.m_bIsDirty = true;
			a.m_bIsHungry = true;
			a.m_iHealth = a.animalInfo.m_sHealth - 1;
			a.m_iSmart = 0;
			
			TileMap.instance.loadMap(TileMap.GARDEN_MAP);
			int tmpY = TileMap.m_iHeightTile * TileMap.instance.m_gardenMap.m_iMapHeight;
            int tmpYY = GameCanvas.h - tmpY*2/3 + 10;
			
            a.m_iLeft = 50;
            a.m_iTop = tmpYY +35;
            a.m_iBoundH = tmpY*2/3 - GameCanvas.hBottomBar - 45;
            a.m_iBoundW = GameCanvas.w -70;
            
            a.start(new Position(a.m_iLeft + CRes.random(20, a.m_iBoundW), a.m_iTop + CRes.random(20, a.m_iBoundH)));
            
            if(m_idFarmOwner == NuoiThuMidlet.m_myPlayerInfo.m_iId && m_arrAnimal.size() == 0){
        		NuoiThuMidlet.m_myPlayerInfo.m_defaultPet = a;
        	}
			m_arrAnimal.addElement(a);
			
			orderAnimalById();
			
			
			NuoiThuMidlet.m_myPlayerInfo.subMoney(moneyBuy, typeMoney);
			NuoiThuMidlet.m_myPlayerInfo.m_sExperience += exp;
		
			/*
			FlyText flyExp = new FlyText();
	        flyExp.startEffect("+" + exp, 20, 20, 0x00ff00, 0x000000);
	        
	        FlyText flyMoney = new FlyText();
	        flyMoney.startEffect("-" + moneyBuy, 30, 30, 0xffff00, 0x000000);
	        */	        
		}
	}

	public void onFeedPet(int gardenID, short animalType, short animalId,
			short health, short foodID, short number) {
		// TODO Auto-generated method stub
		System.out.println(foodID);
		Animal animal = findAnimal(animalType, animalId);
		if(animal != null){
			animal.m_iHealth = health;
			animal.m_bIsHungry = false;
			
			//add effect
			FlyText text = new FlyText();
			text.startEffect("" + health, animal.m_position.x, animal.m_position.y, 0x00ff00, 0x000000);
		
			//sub food in stock
			DataManager.gI().useItem(Item.ITEM_TYPE_FOOD, foodID, number);
		}
	}

	public Animal findAnimal(short animalType, short animalId){
		int i, count = m_arrAnimal.size();
		for(i = 0; i < count; i++){
			Animal a = (Animal)m_arrAnimal.elementAt(i);
			if(a.m_iId == animalId && a.animalInfo.m_iId == animalType){
				return a;
			}
		}	
		return null;
	}
	
	public void onTakeABathPet(int gardenID, short animalType, short animalId) {
		// TODO Auto-generated method stub
		if(gardenID != m_idFarmOwner)
			return;
		
		Animal animal = findAnimal(animalType, animalId);
		if(animal != null){
			animal.m_bIsDirty = false;
		}
	}

	public void onSellPet(byte option, int gardenID, final short animalType,
			final short animalID, short price) {
		// TODO Auto-generated method stub
		Animal animal = findAnimal(animalType, animalID);
		if(animal != null){
			if(option == 0){
				//hoi gia
				GameCanvas.startOKDlg("Giá " + animal.m_strName + " là " + price + " gold. Bạn có muốn bán?", new IAction() {
					
					public void perform() {
						// TODO Auto-generated method stub
						GlobalService.gI().sellPet(1, NuoiThuMidlet.m_myPlayerInfo.m_iId, animalType, animalID);
						
						GameCanvas.endDlg();
					}
				});
			}
			else
			{
				//add money
				FlyText text = new FlyText();
				text.startEffect("" + price, animal.m_position.x, animal.m_position.y, 0x00ff00, 0x000000);
				
				NuoiThuMidlet.m_myPlayerInfo.addMoney(price, 0);
				
				//remove pet 
				m_arrAnimal.removeElement(animal);
				
				orderAnimalById();
			}
		}
	}

	public void onGoDoctor(int gardenID, short animalType, short animalId,
			short sick, short drugID) {
		// TODO Auto-generated method stub
		Animal animal = findAnimal(animalType, animalId);
		if(animal != null){
			//sub item in stock
			DataManager.gI().useItem(Item.ITEM_TYPE_DRUG, drugID, 1);
			
			if(drugID >= 4){
				animal.m_iBonusTime = sick;
				
				int i;
				for(i = 0; i < m_arrSelectAnimal.size(); i++)
				{
					Animal tmp = (Animal)m_arrSelectAnimal.elementAt(i);
					if(tmp.animalInfo.m_iId == animalType && tmp.m_iId == animalId)
						tmp.m_iBonusTime = sick;
				}
				
				return;
			}
			else
			{
				animal.m_iSick = sick;	
			}
												
			//show info dialog
			Animal myAnimal = new Animal(animal.animalInfo.m_iId){
				public void paintInfo(Graphics g, int x, int y){					
					paintIcon(g,x,y);
					BitmapFont.drawNormalFont(g, "Vật nuôi đã được chữa", x+3, y+35 , 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
					BitmapFont.drawNormalFont(g, " bệnh thành công", x+3, y+50 , 0x3a001e, Graphics.LEFT | Graphics.VCENTER);
				}
			};
			
			//gan tat ca cac thuoc tinh cho myAnimal
			myAnimal.m_strName = animal.m_strName;
			//....
			
			Command cRight = new Command(GameResource.exit, new IAction() {
				public void perform(){
					InfoScr.gI().close();
				}
			});
			
			InfoScr.gI().switchToMe("Thông báo", 160, 160, myAnimal, null, cRight, null);
		}
	}

	public void onHarvestMoney(int gardenID, short animalType, short animalId,
			int money) {
		// TODO Auto-generated method stub
		Animal animal = findAnimal(animalType, animalId);
		if(animal != null){
			//add money
			FlyText text = new FlyText();
			text.startEffect("+" + money, animal.m_position.x, animal.m_position.y, 0x00ff00, 0x000000);
			
			NuoiThuMidlet.m_myPlayerInfo.addMoney(money, 0);
		}
	}

	public void onChangeAvatar(short avatarId) {
		// TODO Auto-generated method stub
		//lay avatar moi ra xai
		DataManager.gI().useAvatar(avatarId);
				
		//cat avatar cu vao kho
		Avatar ava = new Avatar((short)NuoiThuMidlet.m_myPlayerInfo.getAvatarId());
		ava.m_iId = ava.itemInfo.m_iId;
		ava.m_iNumItem = 1;
		DataManager.gI().m_myLstAvatar.addElement(ava);
						
		NuoiThuMidlet.m_myPlayerInfo.setAvatar(avatarId);
		GameCanvas.startOKDlg("Bạn đã đổi avatar thành công");
	}

	public void onBuyToy(byte itemType, short itemID, short toyId,
			byte typeMoney, int moneyBuy, int exp) {
		// TODO Auto-generated method stub
		if(itemType == Item.ITEM_TYPE_TOY){
			Toy toy = new Toy(itemID);
			toy.m_iId = toyId;
			toy.m_strName = toy.itemInfo.m_strName + " " + toy.m_iId;
			toy.m_iNumItem = 1;
			
			DataManager.gI().addToyToContainer(toy);
            			
			NuoiThuMidlet.m_myPlayerInfo.subMoney(moneyBuy, typeMoney);
			NuoiThuMidlet.m_myPlayerInfo.m_sExperience += exp;
		
			/*
			FlyText flyExp = new FlyText();
	        flyExp.startEffect("+" + exp, 20, 20, 0x00ff00, 0x000000);
	        
	        FlyText flyMoney = new FlyText();
	        flyMoney.startEffect("-" + moneyBuy, 30, 30, 0xffff00, 0x000000);
	        */
		}
	}


	public void onSetToy(int gardenID, short toyInfoId, short toyId,
			short tileX, short tileY) {
		// TODO Auto-generated method stub
		GameCanvas.endDlg();		
		
		if(tileX == -1 && tileY == -1)
			return;
		
		//add toy vao vuon
		if(!m_arrToy.contains(m_selectedToy)){
			m_arrToy.addElement(m_selectedToy);
		}
		
		m_selectedToy.m_X = m_selectedToy.m_iTileX * TileMap.m_iWidthTile;
		m_selectedToy.m_Y = GameCanvas.h - GameCanvas.hBottomBar - m_selectedToy.m_iTileY * TileMap.m_iHeightTile;
		
		m_arrToy = orderToy(m_arrToy);
				
		//remove toy from stock
		DataManager.gI().m_myLstToy.removeElement(m_selectedToy);
		
		m_selectedToy.m_bIsFocus = false;
		m_selectedToy = null;
		m_bIsSelectedToy = false;
		
		m_cmdCenter = null;
		m_cmdCenter = mainControlCmd;
		
		m_cmdRight = null;
	}

	//////////////////bat dau hieu ung quay so
	public void onStartQuaySo() {
		// TODO Auto-generated method stub
		//m_iRotate = 0;
		m_iSpeed = 2;
		m_iTickQuaySo = 0;
		m_bIsStopRotate = false;
		m_bIsHaveQuaySo = true;
		m_bIsStarQuay = false;
		m_cmdCenter = starQuaySoCmd;
		m_cmdRight = exitQuaySocmd;
		m_iTimeEndQuaySo = CRes.random(20, 30);
	}
	
	int m_iRotate = 0;
	int m_iSpeed = 2;
	int m_iTickQuaySo = 0;
	int m_iTimeEndQuaySo = CRes.random(20, 30);
	short m_sGoldQuaySo;
	boolean m_bIsStopRotate = false;
	boolean m_bIsHaveQuaySo = false;
	boolean m_bIsStarQuay = false;
	//present when play
	byte m_pItemType;
	short m_pItemID; 
	short m_pNum;
	short m_pOption1;
	short m_pOption2;
	
	public void drawQuaySo(Graphics g){
		if(m_bIsHaveQuaySo){
			int tmpY = GameCanvas.h - GameCanvas.hBottomBar;
			int tmpX = GameCanvas.hw;
			int i, x, y, number = 0, r = GameCanvas.w2d3>>1;
			PaintPopup.drawBanQuaySo(g, tmpX - (r), tmpY - (r), m_iRotate);
			r -= 10;
			
			number = m_iRotate/45+1;
			if(m_bIsStopRotate){
				m_cmdCenter = nextQuaySoCmd;
				m_cmdRight = exitQuaySocmd;
				
				if(number == 1 || number == 5)
					number += 3;
				else if(number == 8 || number == 4)
					number -= 3;
				else if(number == 2 || number == 6)
					number ++;
				else 
					number --;
			}
			
			for(i = 0 ; i < 8; i++){
				x = (int)(Math.sin(22.5*Math.PI/180 + i*45*Math.PI/180 + m_iRotate*Math.PI/180)*r + tmpX);
				y = (int)(Math.cos(22.5*Math.PI/180 + i*45*Math.PI/180 + m_iRotate*Math.PI/180)*r + tmpY);
				if(m_bIsStopRotate && number == (i+1)){
					m_iTickQuaySo ++;
					if(m_iTickQuaySo % 100 == 0)
						effectWinning(x, y);
					if(m_iTickQuaySo % 101 == 0)
						effectWinning(x, y);
					if(m_iTickQuaySo % 102 == 0)
						effectWinning(x, y);
					switch (m_pItemType) {
					case Item.ITEM_TYPE_ANIMAL:
						GameResource.instance.m_frameIconItems.drawFrame(1, x, y, Sprite.TRANS_NONE, Graphics.VCENTER|Graphics.HCENTER, g);
						break;
					case Item.ITEM_TYPE_AVATAR:
						GameResource.instance.m_frameIconItems.drawFrame(3, x, y, Sprite.TRANS_NONE, Graphics.VCENTER|Graphics.HCENTER, g);
						break;
					case Item.ITEM_TYPE_DRUG:
						GameResource.instance.m_frameImgShopIcon.drawFrame(2, x, y, Sprite.TRANS_NONE, Graphics.VCENTER|Graphics.HCENTER, g);
						break;
					case Item.ITEM_TYPE_FOOD:
						GameResource.instance.m_frameIconItems.drawFrame(6, x, y, Sprite.TRANS_NONE, Graphics.VCENTER|Graphics.HCENTER, g);
						break;
					case Item.ITEM_TYPE_TOY:
						GameResource.instance.m_frameIconItems.drawFrame(5, x, y, Sprite.TRANS_NONE, Graphics.VCENTER|Graphics.HCENTER, g);
						break;
					default:// không có gì
						GameResource.instance.m_frameEmotion.drawFrame(8, x, y, Sprite.TRANS_NONE, Graphics.VCENTER|Graphics.HCENTER, g);
						break;
					}
				}
				else{
					g.drawImage(GameResource.instance.imgQuestion, x, y, Graphics.VCENTER|Graphics.HCENTER);
					g.setColor(0xff0000);
				}
	    	}
		}
	}
	
	public void effectWinning(int x, int y){
		ExplosionStar e = new ExplosionStar();
    	e.startEffect(x, y, CRes.random(36, 72), 50);

		Smoke s = new Smoke();
		
		s = new Smoke();
		s.startEffect(x, y, CRes.random(4), CRes.random(-8, -5), (byte)20, Smoke.EFFECTS);
		s = new Smoke();
		s.startEffect(x, y, CRes.random(4), CRes.random(-8, -5), (byte)20, Smoke.EFFECTS1);
		s = new Smoke();
		s.startEffect(x, y, CRes.random(4), CRes.random(-8, -5), (byte)20, Smoke.EFFECTS3);
		s = new Smoke();
		s.startEffect(x, y, CRes.random(4), CRes.random(-8, -5), (byte)20 ,Smoke.EFFECTS2);	
		s = new Smoke();
		s.startEffect(x, y, CRes.random(4), CRes.random(-8, -5), (byte)20, Smoke.EFFECTS);
		
		e = new ExplosionStar();
    	e.startEffect(x, y, CRes.random(36, 72), 20);
    	e = new ExplosionStar();
    	e.startEffect(x, y, CRes.random(36, 72), 20);
	}
	
	public void updateQuaySo(){
		if(m_bIsStarQuay && m_bIsHaveQuaySo){
			m_iTickQuaySo ++;
			m_iRotate %= 360;
			m_iRotate += m_iSpeed;
			if(m_iTickQuaySo<m_iTimeEndQuaySo && m_iTickQuaySo%10 == 0){
				m_iSpeed += 20;
			}
			if(m_iTickQuaySo > m_iTimeEndQuaySo && m_iSpeed > 0 && m_iTickQuaySo%10 == 0){
				m_iSpeed -= 3;
			} 
			
			if(m_iSpeed < 2){
				if(m_iRotate%45 != 22 || m_iRotate%45 != 23){
					m_iSpeed = 1;
				}
				if(m_iRotate%45 == 22 || m_iRotate%45 == 23){
					m_bIsStopRotate = true;
					m_bIsStarQuay = false;
					m_iTickQuaySo = 100;
					showGift();
				}
	        }
			
			if(m_iTickQuaySo % 10 == 0){
				int type = (CRes.random(0, 2));
				int x = CRes.random((GameCanvas.w2d3>>1)-50, (GameCanvas.w2d3>>1));
				x = x*((type == 0)?-1:1);
				type = (CRes.random(0, 2));
				int y = CRes.random((GameCanvas.w2d3>>1)-50, (GameCanvas.w2d3>>1));
				y = y*((type == 0)?-1:1);
				Smoke s = new Smoke();
				s.startEffect(GameCanvas.hw + x, GameCanvas.h- GameCanvas.hBottomBar+ y, CRes.random(0, 4), CRes.random(0, 3), (byte)10, Smoke.EFFECTS);
				s = new Smoke();
				s.startEffect(GameCanvas.hw + x, GameCanvas.h- GameCanvas.hBottomBar+ y, CRes.random(0, 4), CRes.random(0, 3), (byte)10, Smoke.EFFECTS1);
				s = new Smoke();
				s.startEffect(GameCanvas.hw + x, GameCanvas.h- GameCanvas.hBottomBar+ y, CRes.random(0, 4), CRes.random(0, 3), (byte)10, Smoke.EFFECTS2);
				s = new Smoke();
				s.startEffect(GameCanvas.hw + x, GameCanvas.h- GameCanvas.hBottomBar+ y, CRes.random(0, 4), CRes.random(0, 3), (byte)10, Smoke.EFFECTS3);
			}
		}
	}

	public void onReceivedPriceQuaySo(final int gold, final int dina) {
		// TODO Auto-generated method stub
		GameCanvas.endDlg();
		
    	Vector vt = new Vector();
    	Command c = new Command(gold + " gold", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				if(NuoiThuMidlet.m_myPlayerInfo.m_iMoney[0] < gold){
					GameCanvas.startOKDlg(GameResource.notEnoughMoney);
					return;
				}
				m_sGoldQuaySo = 0;
				//GlobalService.gI().doQuaySo(m_sGoldQuaySo);
				MainScr.gI().onStartQuaySo();
				
			}
		});
    	vt.addElement(c);
    	
    	c = new Command(dina + " dina", new IAction() {
			
			public void perform() {
				// TODO Auto-generated method stub
				if(NuoiThuMidlet.m_myPlayerInfo.m_iMoney[1] < dina){
					GameCanvas.startOKDlg(GameResource.notEnoughMoney);
					return;
				}
				//GlobalService.gI().doQuaySo(1);
				m_sGoldQuaySo = 1;
				MainScr.gI().onStartQuaySo();
				
			}
		});
    	vt.addElement(c);
    	
    	GameCanvas.menu.startAt(vt, 1);
    	
	}

	public void onReceivedGift(byte itemType, short itemID, short num, short option1, short option2) {
		// TODO Auto-generated method stub
		m_pItemType = itemType;
		m_pItemID = itemID; 
		m_pNum = num;
		m_pOption1 = option1;
		m_pOption2 = option2;
	}
	
	public void showGift(){
		String str = "";
		switch(m_pItemType){
		case Item.ITEM_TYPE_ANIMAL:
			Animal a = new Animal(m_pItemID);
			a.m_iId = m_pOption1;
			a.m_strName = a.animalInfo.m_strName + " " + a.m_iId;
			a.m_iGender = m_pOption2;		
			a.m_bIsDirty = true;
			a.m_bIsHungry = true;
			a.m_iHealth = a.animalInfo.m_sHealth - 1;
			a.m_iSmart = 0;
			
			TileMap.instance.loadMap(TileMap.GARDEN_MAP);
			int tmpY = TileMap.m_iHeightTile * TileMap.instance.m_gardenMap.m_iMapHeight;
            int tmpYY = GameCanvas.h - tmpY*2/3 + 10;
			
            a.m_iLeft = 50;
            a.m_iTop = tmpYY +35;
            a.m_iBoundH = tmpY*2/3 - GameCanvas.hBottomBar - 45;
            a.m_iBoundW = GameCanvas.w -70;
            
            a.start(new Position(a.m_iLeft + CRes.random(20, a.m_iBoundW), a.m_iTop + CRes.random(20, a.m_iBoundH)));
            
            if(m_idFarmOwner == NuoiThuMidlet.m_myPlayerInfo.m_iId && m_arrAnimal.size() == 0){
        		NuoiThuMidlet.m_myPlayerInfo.m_defaultPet = a;
        	}
			m_arrAnimal.addElement(a);
			
			orderAnimalById();
			
			str = "Chúc mừng bạn trúng được 1 con " + a.animalInfo.m_strName;
			break;
		case Item.ITEM_TYPE_AVATAR:
			Avatar ava = new Avatar(m_pItemID);
        	ava.m_iNumItem = 1;
        	ava.m_iId = m_pItemID;
        	DataManager.gI().m_myLstAvatar.addElement(ava);
			str = "Chúc mừng bạn trúng được avatar";
			break;
		case Item.ITEM_TYPE_DRUG:
			DrugInfo dInfo = DataManager.gI().getDrugInfo(m_pItemID);
			if(dInfo.m_iId <= 3)
				str = "Chúc mừng bạn trúng được " + m_pNum + " bịch thuốc chữa " + dInfo.m_strName;
			else
				str = "Chúc mừng bạn trúng được " + m_pNum + " " + dInfo.m_strName;
			DataManager.gI().addItemToContainer(m_pItemType, m_pItemID, m_pNum);
			break;
		case Item.ITEM_TYPE_FOOD:
			FoodInfo fInfo = DataManager.gI().getFoodInfo(m_pItemID);
			str = "Chúc mừng bạn trúng được " + m_pNum + " bịch " + fInfo.m_strName;
			
			DataManager.gI().addItemToContainer(m_pItemType, m_pItemID, m_pNum);
			break;
		case Item.ITEM_TYPE_TOY:
			Toy toy = new Toy(m_pItemID);
			toy.m_iId = m_pOption1;
			toy.m_strName = toy.itemInfo.m_strName + " " + toy.m_iId;
			toy.m_iNumItem = 1;
			
			DataManager.gI().addToyToContainer(toy);
			str = "Chúc mừng bạn trúng được 1 " + toy.itemInfo.m_strName;
			break;
		}
		
		GameCanvas.startOKDlg(str);
	}
	
	public void onReceivedForumLink(String link) {
		// TODO Auto-generated method stub
		DataManager.gI().m_strForumLink = link;
		try {
            
            NuoiThuMidlet.instance.platformRequest(link);
            close();
            NuoiThuMidlet.instance.notifyDestroyed();
        } catch (ConnectionNotFoundException e) {
            e.printStackTrace();
        }
	}
}